package com.vader.common.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

/**
 * @param <T>
 * @author kaivader
 */
public final class CommonResponse<T> implements Serializable {
    private static final long serialVersionUID = 7824278330465676943L;

    private static final int SUCCESS_CODE = 0;

    private static final String SUCCESS_MSG = "success";
    /**
     * 响应编码
     */
    @JSONField(serialzeFeatures = {SerializerFeature.WriteMapNullValue}, ordinal = 1)
    private Integer code;

    /**
     * 响应提示
     */
    @JSONField(serialzeFeatures = {SerializerFeature.WriteMapNullValue}, ordinal = 2)
    private String msg;

    /**
     * 返回的数据
     */
    @JSONField(serialzeFeatures = {SerializerFeature.WriteMapNullValue}, ordinal = 10)
    private T data;

    public static CommonResponse<Object> success() {
        return initData(SUCCESS_CODE, SUCCESS_MSG, null);
    }


    public static CommonResponse<Object> error(String msg) {
        return initData(-1, "系统错误", null);
    }

    public static CommonResponse<Object> error(Integer code, String msg) {
        return initData(code, msg, null);
    }

    public static <T> CommonResponse success(T t) {
        return initData(SUCCESS_CODE, SUCCESS_MSG, t);
    }

    public static <T> CommonResponse errorData(Integer code, T data) {
        return initData(code, "系统错误", data);
    }

    public static <T> CommonResponse errorData(Integer code, String msg, T data) {
        return initData(code, msg, data);
    }

    private static <T> CommonResponse initData(Integer code, String msg, T t) {
        CommonResponse data = new CommonResponse(SUCCESS_CODE);
        if (!isBlank(msg)) {
            data.setMsg(msg);
        }
        if (!isBlank(code.toString())) {
            data.setCode(code);
        }
        data.setCode(code);
        if (t != null) {
            data.setData(t);
        }
        return data;
    }

    private static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }
    }

    public CommonResponse() {
    }

    public CommonResponse(Integer code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

